package com.wwu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwu.common.utils.AssertUtil;
import com.wwu.common.web.ResultPage;
import com.wwu.system.entity.SysQuartzLog;
import com.wwu.system.mapper.SysQuartzLogMapper;
import com.wwu.system.service.ISysQuartzLogService;
import com.wwu.system.vo.QueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * 定时任务日志服务实现类
 * @author 一蓑烟雨
 * @version 1.0.0
 * @date 2023-08-13 18:09
 */
@Slf4j
@Service
public class SysQuartzLogServiceImpl extends ServiceImpl<SysQuartzLogMapper, SysQuartzLog> implements ISysQuartzLogService {

    /**
     * 分页查询定时任务日志
     * @param pageNum  页码
     * @param pageSize  每页数量
     * @param sysQuartzLog 定时任务日志实体
     * @param queryVO   前端查询条件封装实体类
     * @return com.wwu.common.web.ResultPage<com.wwu.system.entity.SysQuartzLog>
     * @author 一蓑烟雨
     * @date 2023/8/13 18:05
     */
    @Override
    public ResultPage<SysQuartzLog> getQuartLogListPage(Integer pageNum, Integer pageSize, SysQuartzLog sysQuartzLog, QueryVO queryVO) {
        Page<SysQuartzLog> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SysQuartzLog> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(sysQuartzLog.getQuartzId())){
            wrapper.eq(SysQuartzLog::getQuartzId,sysQuartzLog.getQuartzId());
        }
        if(StringUtils.hasText(queryVO.getStartDate()) && StringUtils.hasText(queryVO.getEndDate())){
            wrapper.between(SysQuartzLog::getExecTime,queryVO.getStartDate(),queryVO.getEndDate());
        }
        wrapper.orderByDesc(SysQuartzLog::getQuartzId).orderByDesc(SysQuartzLog::getExecTime);

        IPage<SysQuartzLog> sysQuartzLogPage = this.page(page, wrapper);
        return new ResultPage<>(sysQuartzLogPage);
    }

    /**
     * 添加定时任务日志
     * @param sysQuartzLog 定时任务日志实体
     * @author 一蓑烟雨
     * @date 2023/8/13 18:01
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveQuartzLog(SysQuartzLog sysQuartzLog) {
        AssertUtil.isTrue(!StringUtils.hasText(sysQuartzLog.getQuartzId()),"任务id不能为空");
        AssertUtil.isTrue(!StringUtils.hasText(sysQuartzLog.getQuartzClass()),"任务执行类不能为空");
        AssertUtil.isTrue(ObjectUtils.isEmpty(sysQuartzLog.getExecStatus()),"任务状态不能为空");
        AssertUtil.isTrue(!(this.save(sysQuartzLog)),"定时任务日志添加失败！");
    }

    /**
     * 根据任务id批量删除定时任务
     * @param quartzIds 任务id集合
     * @author 一蓑烟雨
     * @date 2023/8/13 18:03
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteQuartzLogByIds(List<String> quartzIds) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(quartzIds),"任务id不能为空");

        LambdaQueryWrapper<SysQuartzLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysQuartzLog::getQuartzId,quartzIds);
        //如果数量大于0则执行删除操作
        if(this.count(wrapper) > 0){
            AssertUtil.isTrue(!this.remove(wrapper),"定时任务日志删除失败！");
        }
    }
}
